var utils = require('../../../../utils/util.js')
var c = require('../../../../utils/config.js')
var p = require('../../../../utils/password.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    level_id: "",
    need_money: 0.00,
    need_pay: 0.00,
    userinfo: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      level_id: options.level_id,
      need_money: options.need_money,
    });

    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var data = {
      url: "/yluser/user/gettokenuserinfo",
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        let data = res.data;
        let need_pay = 0;
        if (that.data.need_money - data.money > 0) {
          need_pay = that.data.need_money - data.money;
        }
        that.setData({
          userinfo: data,
          need_pay: need_pay.toFixed(2),
        });
      } else {

      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  tab_pay: function (e) {
    let k = e.currentTarget.dataset.index;
    this.setData({
      select: k
    })
  },
  paypwd_btn: function () {
    this.setData({
      is_show: true
    })
  },
  bindKeyInput: function (e) {
    let value = e.detail.value
    let key = e.target.dataset.index;
    this.setData({
      password: value
    })
  },
  close_pwd: function () {
    this.setData({
      is_show: false
    })
  },
  submit: function () {
    var level_id = this.data.level_id;
    var pay_type = 0;//微信
    var user_sign = "";
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var paypwd = this.data.password;
    if (!paypwd || paypwd < 100000) {
      utils.errMsg("请输入支付密码");
      return false;
    }
    var data = {
      url: "/zfshop/userupgrade/add",
      level_id: level_id,
      token: token,
      pay_type: pay_type,
      user_sign: user_sign,
      openid: wx.getStorageSync("openId"),
      spassword: p.encryption(paypwd)
    };

    utils.request(data).then(function (res) {
      if (res.code == 0) {
        //直接支付成功!
        if (res.data.length == 0) {
          utils.sucMsg("支付成功");
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 2000);
          return false;
        }
        //走在线支付
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            utils.sucMsg("支付成功");
            setTimeout(function () {
              wx.navigateBack({
                delta: 1,
              })
            }, 2000);
          },
          'fail': function (res) {
            utils.errMsg("支付失败");
            setTimeout(function () {
              wx.navigateBack({
                delta: 1,
              })
            }, 2000);
          }
        })
      } else {
        utils.errMsg(res.msg);
      }
    });
  }

})